﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatingObject
{
    class CreatingObject
    {
        static void Main()
        {
            NorthwindEntities nWEntity = new NorthwindEntities();
            string customerID = "23456";
            string companyName = "Brand New Company";
            string contactName = null;
            string contactTitle = null;
            string address = null;
            string city = null;
            string region = null;
            string postalCode = null;
            string country = null;
            string phone = null;
            string fax = null;
            //InsertCustomer(nWEntity, customerID, companyName, contactName, contactTitle,
            //    address, city, region, postalCode, country, phone, fax);

            //string newName = "Stanislav Savov";
            //string newCity = "Sofia";
            //ModifyCustomer(nWEntity, newName, newCity, customerID);

            string deletedCompanyID = "23456";
            DeletingCustomer(nWEntity, deletedCompanyID);
        }

        private static void ModifyCustomer(NorthwindEntities nWEntity, string newName, string newCity, string customerID)
        {
            Customer customer = nWEntity.Customers.FirstOrDefault(c => c.CustomerID == customerID);
            customer.ContactName = newName;
            customer.City = newCity;
            nWEntity.SaveChanges();
        }

        private static void DeletingCustomer(NorthwindEntities nWEntity, string customerID)
        {
            try
            {
                Customer customer = nWEntity.Customers.FirstOrDefault(c => c.CustomerID == customerID);
                nWEntity.Attach(customer);
                nWEntity.Customers.DeleteObject(customer);
                nWEntity.SaveChanges();
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Customer not found.");
            }
        }

        private static void InsertCustomer(NorthwindEntities nWEntity, string customerID, string companyName, 
            string contactName, string contactTitle, string address, string city, string region, string postalCode, 
            string country, string phone, string fax)
        {
            Customer customer = new Customer()
            {
                CustomerID = customerID,
                CompanyName = companyName,
                ContactName = contactName,
                ContactTitle = contactTitle,
                Address = address,
                City = city,
                Region = region,
                PostalCode = postalCode,
                Country = country,
                Phone = phone,
                Fax = fax
            };
            nWEntity.Customers.AddObject(customer);
            nWEntity.SaveChanges();
        }
    }
}
